echo "开始运行！"
cd /opt/softwar
# 校验安装，应显示 libaio 版本
mariadb=`rpm -qa | grep mariadb`
if [ -z "$mariadb" ]; then
    echo "mariadb已经卸载！"
else
    rpm -e  $mariadb --nodeps
echo "卸载mariadb,成功！"
fi
#安装 libaio
libaio=`rpm -qa | grep libaio`
if [ -z "$libaio" ]; then
   rpm -ivh libaio-*.rpm --nodeps --force   # 忽略依赖校验
fi
numactl=`rpm -qa | grep numactl`
if [ -z "$numactl" ]; then
   rpm -ivh numactl-*.rpm --nodeps --force   # 忽略依赖校验
fi
if getent group mysql>/dev/null; then
   echo "mysql，用户组存在跳过添加"
else
   sudo groupadd  mysql
   echo "mysql，用户组创建成功!"
fi
username=mysql
group_name=mysql
if id -u  $username >/dev/null 2>&1; then
   echo "$username，用户存在跳过添加"
else
   #创建一个用户，其中-d和-m选项用来为登录名在/home目录下创建一个目录
   #-s 登录shell是， -g 用户主用户组
   sudo useradd -g $group_name  $username
   if [ $? -eq 0 ]; then
     echo "用户 $username 创建成功。"
   else
     echo "错误：创建用户 $username 失败。" 
     exit 1
   fi
fi
if [ -e /opt/module/mysql ]; then
   rm -rf /opt/module/mysql
   echo "/opt/module/mysql,已删除"
fi
sudo mkdir -p /opt/module/mysql
echo "/opt/module/mysql,创建目录成功！"
if [ -e /opt/module/mysql/data ]; then
   rm -rf /opt/module/mysql/data
   echo "/opt/module/mysql/data,已删除"
fi
sudo mkdir -p /opt/module/mysql/data
echo "/opt/module/mysql/data,创建目录成功！"
#查看mysql的安装包
mysql_tar=`find /opt/softwar -name mysql-8.0*`
if [[ $mysql_tar == *"mysql-8.0"* ]]; then
    echo "mysql_tar = $mysql_tar"
else
    echo "mysql_tar，值为空,退出安装"
    exit 1
fi
tar -xvf $mysql_tar -C /opt/module/mysql
sudo chown -R mysql:mysql /opt/module/mysql
sudo chmod -R 775 /opt/module/mysql
cd /opt/module/mysql
#查询安装后的目录
install_path=`find /opt/module/mysql -name mysql-8.0*`
if [[ $install_path == *"mysql-8.0"* ]]; then
    echo "install_path = $install_path"
else
    echo "install_path，值为空,退出安装"
    exit 1
fi
sudo $install_path/bin/mysqld --initialize --user=mysql --basedir=$install_path --datadir=/opt/module/mysql/data
sudo cat > /etc/my.cnf << EOF
[mysqld]
basedir=$install_path
datadir=/opt/module/mysql/data
port = 3306
socket=/tmp/mysql.sock
bind-address=0.0.0.0
log-error=/opt/module/mysql/mysqld.log
pid-file=/opt/module/mysql/mysqld.pid
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
#跳过密码认证
skip-grant-tables
[client]
default-character-set=utf8mb4
EOF
echo "查看/ect/my.cnf配置"
cat /etc/my.cnf
sudo echo "#环境变量" > /etc/profile.d/mysql_env.sh
sudo echo "export MYSQL_HOME=$install_path" >> /etc/profile.d/mysql_env.sh
sudo echo "export PATH=\$PATH:\$MYSQL_HOME/bin" >> /etc/profile.d/mysql_env.sh
#环境变量生效
cat /etc/profile.d/mysql_env.sh
source /etc/profile
#系统默认查找路径/usr/bin下无mysql命令链接
if [ -e /usr/bin/mysql ]; then
    echo "/usr/bin/mysql,已存在则删除！"
    unlink /usr/bin/mysql
else
    ln -s $install_path/bin/mysql /usr/bin/mysql
    ls -l /usr/bin/mysql
fi
#添加到自启动
sudo cp $install_path/support-files/mysql.server /etc/init.d/mysqld
sudo sed -i "46s|basedir=|basedir=$install_path|" /etc/init.d/mysqld
sudo sed -i "47s|datadir=|datadir=/opt/module/mysql/data|g" /etc/init.d/mysqld
# 添加服务
sudo chkconfig --add mysqld 
echo " 开机自启"
sudo chkconfig mysqld on
echo " 启动服务"
sudo service mysqld start 
echo "验证服务状态"
sudo service mysqld status 
mysql -u root <<EOF
use mysql;
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' IDENTIFIED BY '123123';
FLUSH PRIVILEGES;
EOF
echo "root密码是123123"
echo " 关闭服务"
sudo service mysqld stop
echo " 免密功能关闭"
sudo sed -i "s/skip-grant-tables/#skip-grant-tables/g" /etc/my.cnf
echo " 启动服务"
sudo service mysqld start 
mysql -u root -p123123<<EOF
use mysql;
FLUSH PRIVILEGES;
ALTER USER 'root'@'%' IDENTIFIED  WITH mysql_native_password BY '123123';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
echo " Mysql允许远程登录"
# 防火墙配置
echo "防火墙允许3306端口通过"
sudo firewall-cmd --add-port=3306/tcp --permanent
sudo firewall-cmd --reload
echo "运行结束！"